// pages/mine/mine.js
import {
  USER_INFO
} from '../../request/api.js'

//获取应用实例
var app

Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: '点击头像登录',
    userInfo: {},
    isLogin: false,
    reload: false,
    medals: [],
    items: [{
        'count': 0,
        'name': '动弹',
        'path': '../tweet/my/tweets'
      },
      {
        'count': 0,
        'name': '收藏',
        'path': '../my/collection/collection'
      },
      {
        'count': 0,
        'name': '关注',
        'path': '../my/relation/relation?type=1'
      },
      {
        'count': 0,
        'name': '粉丝',
        'path': '../my/relation/relation?type=2'
      },
    ],
    mys: [
      // {'iconPath':'/images/ic_my_message.png', 'name':'我的消息', 'path':'../login/login'},
      // {'iconPath':'/images/ic_my_badge.png', 'name':'我的勋章', 'path':'../login/login'},
      {
        show: true,
        'iconPath': '/images/ic_my_profile.png',
        'name': '阅读记录',
        'path': '../my/read_record/readRecord'
      },
      {
        show: true,
        'iconPath': '/images/ic_my_blog.png',
        'name': '我的博客',
        'path': '../my/blog/blog'
      },
      {
        show: true,
        'iconPath': '/images/ic_my_blacklist.png',
        'name': '我的黑名单',
        'path': '../my/relation/relation?type=3'
      },
      {
        show: false,
        'iconPath':'/images/ic_my_question.png', 
        'name':'我的问答', 
        'path':'../my/question_answer/question_answer'
      },
      // {'iconPath':'/images/ic_my_publish.png', 'name':'我的投递', 'path':'../login/login'},
      // {'iconPath':'/images/ic_my_event.png', 'name':'我的活动', 'path':'../login/login'},
      // {'iconPath':'/images/ic_my_tags.png', 'name':'关注标签', 'path':'../login/login'},
      // {'iconPath':'/images/ic_my_recommend.png', 'name':'邀请好友', 'path':'share'},
    ],
    radar: [0, 0, 0, 0, 0, 0],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    app = getApp();
    this.getUserInfo();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var reload = this.data.isLogin != app.globalData.isLogin;
    this.setMyQuestionAnswerVisbalbe(app.globalData.versionCheckPassed && app.globalData.isLogin);
    var that = this;
    if (reload) {
      if (!app.globalData.isLogin) {
        this.setData({
          userInfo: {},
          isLogin: false,
          medals: [],
          radar: [0, 0, 0, 0, 0, 0],
          mys: that.data.mys,
          items: [{
              'count': 0,
              'name': '动弹',
              'path': '../tweet/my/tweets'
            },
            {
              'count': 0,
              'name': '收藏',
              'path': '../my/collection/collection'
            },
            {
              'count': 0,
              'name': '关注',
              'path': '../my/relation/relation?type=1'
            },
            {
              'count': 0,
              'name': '粉丝',
              'path': '../my/relation/relation?type=2'
            },
          ],
        });
      } else {
        that.setData({
          mys: that.data.mys,
        })
      }
      this.getUserInfo();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getUserInfo();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (options) {
    var that = this;
    // 设置菜单中的转发按钮触发转发事件时的转发内容
    var userId = that.data.userInfo.id || '';
    var shareObj = {
      title: that.getShareTitle(), // 默认是小程序的名称(可以写slogan等)
      path: '/pages/user/user?userId=' + userId + '&share=' + 1, // 默认是当前页面，必须是以‘/’开头的完整路径
      // imageUrl: '', //自定义图片路径，可以是本地文件路径、代码包文件路径或者网络图片路径，支持PNG及JPG，不传入 imageUrl 则使用默认截图。显示图片长宽比是 5:4
      success: function (res) {
        // 转发成功之后的回调
        if (res.errMsg == 'shareAppMessage:ok') {}
      },
    };
    return shareObj;

  },

  onShareTimeline: function () {
    var that = this;
    var userId = that.data.userInfo.id || '';
    // 设置菜单中的转发按钮触发转发事件时的转发内容
    var shareObj = {
      title: that.getShareTitle(), // 默认是小程序的名称(可以写slogan等)
      query: 'userId=' + userId + '&share=' + 1, //当前页面路径携带的参数
      imageUrl: that.data.userInfo.portrait,
      success: function (res) {
        // 转发成功之后的回调
        if (res.errMsg == 'shareAppMessage:ok') {}
      },
    };
    return shareObj;
  },

  getShareTitle: function () {
    var userInfo = getApp().globalData.userInfo;
    var userName;
    if(userInfo){
      userName = userInfo.name || '有人';
    } else {
      userName = '有人';
    }
    var title = userName + '分享了个人主页';
    return title;
  },

  setMyQuestionAnswerVisbalbe: function(show){
    this.data.mys[3].show = show;
  },

  getUserInfo: function (e) {
    if (!app.globalData.isLogin) {
      return;
    }
    USER_INFO().then(res => {
      if (res.data.code == 1) {
        var result = res.data.result;
        app.globalData.userInfo = result;
        app.globalData.userId = result.id;
        var statistics = result.statistics;

        var tempItems = this.data.items;
        tempItems[0].count = statistics.tweet;
        tempItems[1].count = statistics.collect;
        tempItems[2].count = statistics.follow;
        tempItems[3].count = statistics.fans;

        var radars = result.more.radar;

        this.setData({
          userInfo: result,
          medals: result.more.medals,
          radar: radars,
          isLogin: true,
          items: tempItems,
          reload: false
        })
      }
    }).catch(res => {

    })
  },

  _checkLogin: function () {
    var app = getApp();
    if (!app.globalData.isLogin) {
      wx.showModal({
        content: '您还没有登录，请登录',
        success: function (res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/account/login/login',
            });
          }
        }
      });
      return false;
    } else {
      return true;
    }
  },

  //跳转
  navigateToCheckLogin: function (path, checkLoginStatus) {
    if (checkLoginStatus && !this._checkLogin()) {
      return;
    }
    wx.navigateTo({
      url: path
    });
  },
  navigateTo: function (path) {
    wx.navigateTo({
      url: path
    })
  },

  navigateToSetting: function () {
    this.navigateTo('../setting/setting');
  },

  navigateToLogin: function () {
    this.navigateTo('../account/login/login');
  },

  navigaToPage: function (e) {
    var path = e.currentTarget.dataset.path;
    this.navigateToCheckLogin(path, true);
  },

  bingAvatarTap: function () {
    if (!this.data.isLogin) {
      this.navigateToLogin();
    }
  },

})